<template>
	<view>
		<image src="../../static/me/bg1.jpg" mode="" style="position: fixed;top: 0; left: 0;width: 100vw; height: 100vh;"></image>
		<view style="z-index: 100;position: absolute;width: 100vw;">
			<view style="display: flex; align-items: center; padding: 5rpx; padding-left: 5vh;">
				<picker @change="changeDorm" :value="dorm" :range="dormList" style="position: relative; top:2px;">
					<button type="primary" size="mini" @click="searchDorm">{{ dorm ? dorm : '选择楼舍↑' }}</button>
				</picker>
				<button type="default" style="position: relative; left: 32rpx;" size="mini" @click="changeStartTime()">
					{{ startTimeChinese ? startTimeChinese : '起始时间↑' }}
				</button>
				<button type="warn" size="mini" @click="reset()">重置</button>
			</view>

			<view v-for="(item, index) in taskLogs" v-bind:key="item.id">
				<view style="border-top: 1px solid #7e7e7e; padding: 10rpx 20rpx;">
					<view>订水寝室 {{ item.dorm + item.room }}</view>
					<view>送出水桶 {{ item.bucketNum }}</view>
					<view>
						<text v-if="item.receiveEmptyBucketNum <= 0" style="color: red;">收回空桶 {{ item.receiveEmptyBucketNum }}</text>
						<text v-else>收回空桶 {{ item.receiveEmptyBucketNum }}</text>
					</view>
					<view>送水时间 {{ item.timestamp }}</view>
					<view>叫水人 {{ item.publisher }}</view>
					<view>送水员 {{ item.workerName }}</view>
					<view class="text-center ">
						<button type="default" size="mini" style="margin: 0 3px;" @click="deleteRecord(index)">记录有误</button>
						<button type="default" size="mini" style="margin: 0;" @click="goToDomitoryHistory">寝室送水记录</button>
					</view>
				</view>
			</view>
			<view style="border-top: 1px solid #7e7e7e;"><uni-pagination :total="total" :current="pageNo" :pageSize="pageSize" @change="chagePage" /></view>
		</view>
	</view>
</template>

<script>
import pageNation from '@/components/uni-pagination/uni-pagination.vue';

// 楼舍的送水记录
export default {
	components: {
		pageNation
	},
	data() {
		return {
			dormList: ['二区15舍'],
			dorm: null,
			taskLogs: [],
			startTimeChinese: '今年',
			startTime: 0,
			currentIndex: 0,
			pageSize: 10,
			pageNo: 1,
			total: 0,
			userInfo: null
		};
	},
	onShow() {
		this.userInfo = uni.getStorageSync("userInfo")
		let dormCache = uni.getStorageSync('workerTaskLogDormLastSelected');
		if (dormCache) this.dorm = dormCache; // 从缓存里获取上次选择的楼舍
		this.startTime = this.parseTimeToLong();
		this.getHistoryLog();
		this.searchDorm();
	},
	// 下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 300);
		this.getHistoryLog();
	},
	methods: {
		// 获取某楼舍的送水记录
		getHistoryLog() {
			let that = this;
			let path =
				'/taskLog/dorm?startTime=' +
				that.startTime +
				(that.dorm ? '&dorm=' + that.dorm : '') +
				'&pageNo=' +
				that.pageNo +
				'&pageSize=' +
				that.pageSize;
			that.$request(path, 'get').then(result => {
				that.taskLogs = result.data.records;
				that.total = result.data.total;
			});
		},
		// 获取所有楼舍
		searchDorm() {
			let that = this;
			that.$request('/address/dorm/list' , 'get').then(result => {
				that.dormList = result.data;
			});
		},
		// 切换为其他楼舍的送水记录
		changeDorm(e) {
			console.log('picker发送选择改变，携带值为', e.target.value);
			this.dorm = this.dormList[e.target.value];
			uni.setStorageSync('workerTaskLogDormLastSelected', this.dorm); // 缓存选择的楼舍供下次查询用
			this.pageNo = 1;
			this.getHistoryLog();
		},
		// 撤销送水记录
		deleteRecord(index) {
			let that = this;
			uni.showModal({
				title: '撤销',
				content: '是否撤销本次送水记录并重新创建送水任务(只能操作当天的记录)',
				success: function(e) {
					if (e.confirm) {
						that.$request('/taskLog/rollback/' + that.taskLogs[index].id, 'delete').then(result => {
							that.getHistoryLog();
							that.$msg('撤销成功', 1000, 'success');
						});
					}
				}
			});
		},
		// 选择起始时间
		changeStartTime() {
			let that = this;
			let list = ['今天', '本周', '本月', '最近三月', '今年', '全部'];
			uni.showActionSheet({
				itemList: list /** 按钮的文字数组，数组长度最大为 6 */,
				success: function(e) {
					that.startTimeChinese = list[e.tapIndex];
					that.startTime = that.parseTimeToLong();
					that.pageNo = 1;
					that.getHistoryLog();
				}
			});
		},
		// 解析时间
		parseTimeToLong() {
			let str = this.startTimeChinese;
			let startTime = new Date();
			startTime.setHours(0, 0, 0);
			if (str === '今天') {
				return startTime.getTime();
			} else if (str === '本周') {
				startTime.setDate(startTime.getDate() - startTime.getDay() + 1); // 本周
			} else if (str === '本月') {
				startTime.setDate(1); // 本月
			} else if (str === '最近三月') {
				startTime.setMonth(startTime.getMonth() - 2);
			} else if (str === '今年') {
				startTime.setMonth(0);
				startTime.setDate(1);
			} else {
				return 0;
			}
			return startTime.getTime();
		},
		reset() {
			this.dorm = null;
			this.startTimeChinese = '今年';
			this.parseTimeToLong();
			this.pageNo = 1;
			this.getHistoryLog();
		},
		// 去寝室历史记录页面
		goToDomitoryHistory() {
			uni.navigateTo({
				url: '/pages/me/dormitory/addressTaskHistory?addressId=' + this.taskLogs[this.currentIndex].addressId
			});
		},
		// 换页
		chagePage(e) {
			this.pageNo = e.current;
			this.getHistoryLog();
		}
	}
};
</script>
<style>
.mask {
	position: fixed;
	top: 0px;
	width: 100vw;
	height: 100vh;
}
.modal {
	position: fixed;
	top: 20vh;
	left: 100rpx;
	padding: 10px 10px 0px;
	width: 500rpx;
	background-color: #eeeeee;
}
</style>
